﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using UniversityStudentManagemnetSystem.DAO.DAL;
using UniversityStudentManagemnetSystem.DAO.DAL.Gateway;

namespace UniversityStudentManagemnetSystem.BLL
{
    public class ViewManager
    {
        ViewGateway viewGateway = new ViewGateway();
        protected enum Rows
        {
            oneRowAffected = 1,
            noRowAffected = 0,
            oneRowSelected = 1,
            noRowSeleced = 0,
            oneRowCounted = 1,
            noRowCounted = 0
        }      
         //For 2ND View (Department,Semester)
        
        public DataTable GetAllUnAssignCourseForDepartmentIdSemesterId(string selectDepartment, string selectSemester, out string message1)
        {
            int semesterId = Convert.ToInt32(selectSemester);
            int departmentId = Convert.ToInt32(selectDepartment);
            DataTable table = viewGateway.GetAllUnAssignCoursesForSemesterAndDepartment(semesterId, departmentId);
            message1 = "Unassigned Courses for selected Department and Semester";
            return table;
        }
                             //For 2ND View (Department)
        public DataTable GetAllUnAssignCourseForDepartment(string selectDepartment, out string message2)
        {
            int departmentId = Convert.ToInt32(selectDepartment);
            DataTable table = viewGateway.GetAllUnAssignCoursesForDepartmentForView(departmentId);
            message2 = "Unassigned Courses for selected Department";
            return table;
        }
     
        //Final Schedule Course View
             DataTable courseScheduleTable = null;

        public DataTable GetALScheduleCourses(string selectDepartment, string selectSemester,out string outStringMessage)
        {
            int selectedDepartment = Convert.ToInt32(selectDepartment);
            int selectedSemester = Convert.ToInt32(selectSemester);
            courseScheduleTable = viewGateway.GetAllScheduleCourseForDepartmentAndSemesterForView(selectedDepartment,selectedSemester);
            DataTable scheduleCourseTable = GetAllCourses(courseScheduleTable);
            scheduleCourseTable = AddCommand(scheduleCourseTable);
            outStringMessage = "All Courses for Selected Semester and Department.";
            return scheduleCourseTable;
        }
        public DataTable GetScheduleCoursesForDepartment(string selectDepartment, out string outStringMessage)
         {
            int selectedDepartment = Convert.ToInt32(selectDepartment);
            courseScheduleTable = viewGateway.GetAllScheduleCoursesForSelectedDepartment(selectedDepartment);
            DataTable scheduleCourseTable = GetAllCourses(courseScheduleTable);
            scheduleCourseTable = AddCommand(scheduleCourseTable);
            outStringMessage = "Scheduled Courses for Selected Department.";
            return scheduleCourseTable;
         }

        private DataTable AddCommand(DataTable scheduleCourseTable)
        {

            foreach (DataRow row in scheduleCourseTable.Rows)
            {
                string assignMesg = @"Not assign yet";
                string scheduleMesg = @"Not scheduled yet";

                if (row["Teacher_Name"].ToString() == "")
                {
                    row["Teacher_Name"] = assignMesg;
                }
                if (row["Detail_Schedule"].ToString() == "")
                {
                    row["Detail_Schedule"] = scheduleMesg;
                }
            }

            return scheduleCourseTable;
       

        }

        private DataTable GetAllCourses(DataTable courseScheduleTable)
        {
          
            courseScheduleTable.Columns.Add("Detail_Schedule", typeof (string));
            foreach (DataRow courseRow in courseScheduleTable.Rows)
            {
                if (courseRow["Day"].ToString()!="")
                {
                    courseRow["Detail_Schedule"] += string.Format("{0} {1} {2} {3} {4} \n", courseRow["Day"], courseRow["BuildingName"],courseRow["Room"],
                        courseRow["StartingTime"],courseRow["EndingTime"]);
                }
            }

            courseScheduleTable.Columns.Remove("DepartmentId");
            courseScheduleTable.Columns.Remove("SemesterId");
            courseScheduleTable.Columns.Remove("StartingTime");
            courseScheduleTable.Columns.Remove("EndingTime");
            courseScheduleTable.Columns.Remove("Room");
            courseScheduleTable.Columns.Remove("BuildingName");
            courseScheduleTable.Columns.Remove("Flag");
            return courseScheduleTable;
        }
        //For 3RD View  (Department,Semester) 

        public DataTable GetAllUnscheduleCourseForDePartmentIdAndSemesterId(string selectDepartment, string selectSemester, out string message1)
        {
            int department = Convert.ToInt32(selectDepartment);
            int semester = Convert.ToInt32(selectSemester);
            message1 = "Unscheduled Courses for Selected Department and Semester.";
            return viewGateway.GetAllUnscheduleCourseForDepartmentAndSemester(department, semester);
        }
        ////For 3RD View (Department)
        public DataTable GetUnScheduleCourseForDepartment(string selectDepartment, out string message2)
        {
            int department = Convert.ToInt32(selectDepartment);
            message2 = "Unscheduled Courses for Selected Department.";
            return viewGateway.GetUnscheduleCourseForDepartmentId(department);
        }
    }
   

}